<template>
  <div class="prodects-index">
    <div class="product-category container">
      <div class="product-category-item" v-if="allCategoryList.length">
        <el-dropdown placement="bottom-start" v-for="item in allCategoryList" :key="item.bigTypeName">
          <div class="category-item">
            <div class="product-image">
              <img :src="item.coverSvg" class="default" />
              <img :src="item.coverJpg" class="hover" />
            </div>
            <p class="category-name">{{ item.bigTypeNameOfLang }}</p>
          </div>
          <el-dropdown-menu class="prodects-dropdown-menu" slot="dropdown">
            <div class="dropdown-menu">
              <div class="menu-title">
                <p>{{ $t("equipmentType") }}</p>
                <p>{{ $t("equipmentModel") }}</p>
              </div>
              <div class="menu-content">
                <div class="content-item" v-for="subItem in item.subCategoryVOList" :key="subItem.subTypeName">
                  <div class="content-img">
                    <el-image class="sub-img" :src="subItem.coverImage" fit="contain"></el-image>
                    <p class="sub-name">{{ subItem.subTypeNameOfLang }}</p>
                  </div>
                  <div class="content-models">
                    <a
                      class="models-sku"
                      v-for="skuitem in subItem.skuList"
                      :key="skuitem.skuId"
                      :href="`/products-detail/${skuitem.skuId}`"
                      >{{ skuitem.skuName }}</a
                    >
                  </div>
                </div>
              </div>
            </div>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <div class="video-banner-wrap">
      <div class="video-banner">
        <div class="video-banner-inner">
          <video
            autoplay=""
            muted="muted"
            preload="auto"
            x5-video-player-type="h5"
            x5-video-orientation="portraint"
            controls360="no"
            raw-controls="true"
            x5-video-player-fullscreen="false"
            x-webkit-airplay="true"
            airplay="allow"
            webkit-playsinline="true"
            playsinline="true"
            loop="loop"
            src="https://yunlian-oss-product.oss-cn-hangzhou.aliyuncs.com/website/product.mp4"
            poster="https://yunlian-oss-product.oss-cn-hangzhou.aliyuncs.com/website/p.jpg"
          ></video>
        </div>
      </div>
    </div>
    <div class="category-detail container" v-if="categoryItemList.length">
      <div class="category-item" v-for="(item, index) in categoryItemList" :key="item.bigTypeName">
        <div class="item-title">{{ item.bigTypeNameOfLang }}</div>
        <div class="list-icon">
          <div
            class="icon-outer"
            v-for="(subItem, subIndex) in item.subCategoryVOList"
            :key="subItem.subTypeName"
            @click="handleSubItem(subItem.skuList, index, subIndex)"
          >
            <div class="icon-one">
              <img :alt="subItem.subTypeNameOfLang" :src="subItem.coverImage" />
              <span :class="[item.activeIndex == subIndex ? 'activeText' : '']">{{ subItem.subTypeNameOfLang }}</span>
            </div>
            <div :class="['icon-line', item.activeIndex == subIndex ? 'activeLine' : '']"></div>
          </div>
        </div>
        <div class="spece-compare">{{ $t("specsCompare") }}</div>
        <div class="list-table">
          <el-table :data="item.tableData" style="width: 100%" :row-class-name="tableRowClassName">
            <el-table-column v-for="columnsItem in item.columnsList" :key="columnsItem.label" :prop="columnsItem.prop" :label="columnsItem.label">
            </el-table-column>
            <el-table-column min-width="50">
              <template slot-scope="scope">
                <a :href="`/products-detail/${scope.row.skuId}`">
                  <i class="el-icon-arrow-right arrow-right"></i>
                </a>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
    <!-- 侧边栏服务 -->
    <Dangling v-if="locale == 'zh-CN'" />
    <!-- WhatsApp -->
    <!-- <WhatsApp v-if="locale == 'en-US'" /> -->
    <!-- 在线客服 -->
    <Connect />
  </div>
</template>

<script>
import { mapState } from "vuex"
export default {
  name: "ProductsIndex",
  layout: "Layout",
  components: {},
  data() {
    return {}
  },
  head() {
    return {
      title: this.$i18n.t("headTitle"),
      meta: [
        {
          hid: "keywords",
          name: "keywords",
          content: this.$i18n.t("haeaKeyContent")
        },
        {
          hid: "description",
          name: "description",
          content: this.$i18n.t("haeaDecContent")
        }
      ]
    }
  },
  async asyncData({ app, query }) {
    let allCategoryList = [],
      categoryItemList = [],
      columnsList1 = [],
      tableData1 = [],
      columnsList2 = [],
      tableData2 = [],
      activeIndex = 0
    try {
      let { data } = await app.$api.allCategory()
      allCategoryList = data || []
      // 默认只展示以下两种分类设备
      // Concrete Machinery 混凝土机械
      // Cranes 起重机
      categoryItemList = allCategoryList.filter((v) => v.bigTypeName == "Concrete Machinery" || v.bigTypeName == "Cranes")
      if (categoryItemList.length) {
        categoryItemList[0].activeIndex = activeIndex
        categoryItemList[1].activeIndex = activeIndex
        let newSkuList1 = categoryItemList[0]?.subCategoryVOList[0]?.skuList || []
        let newSkuList2 = categoryItemList[1]?.subCategoryVOList[0]?.skuList || []
        if (newSkuList1.length) {
          columnsList1.push({ label: newSkuList1[0].skuTitle, prop: "model" })
          newSkuList1[0].specsList.map((specsItem, index) => {
            columnsList1.push({ label: specsItem.specsNameOfLang, prop: `specsName${index + 1}` })
          })
          newSkuList1.map((skuItem) => {
            if (skuItem.specsList.length > 2) {
              tableData1.push({
                skuId: skuItem.skuId,
                model: skuItem.skuNameOfLang,
                specsName1: skuItem.specsList[0].specsValueOfLang,
                specsName2: skuItem.specsList[1].specsValueOfLang,
                specsName3: skuItem.specsList[2].specsValueOfLang
              })
            } else {
              tableData1.push({
                skuId: skuItem.skuId,
                model: skuItem.skuNameOfLang,
                specsName1: skuItem.specsList[0].specsValueOfLang,
                specsName2: skuItem.specsList[1].specsValueOfLang
              })
            }
          })
        }
        if (newSkuList2.length) {
          columnsList2.push({ label: newSkuList1[1].skuTitle, prop: "model" })
          newSkuList2[0].specsList.map((specsItem, index) => {
            columnsList2.push({ label: specsItem.specsNameOfLang, prop: `specsName${index + 1}` })
          })
          newSkuList2.map((skuItem) => {
            if (skuItem.specsList.length > 2) {
              tableData2.push({
                skuId: skuItem.skuId,
                model: skuItem.skuNameOfLang,
                specsName1: skuItem.specsList[0].specsValueOfLang,
                specsName2: skuItem.specsList[1].specsValueOfLang,
                specsName3: skuItem.specsList[2].specsValueOfLang
              })
            } else {
              tableData2.push({
                skuId: skuItem.skuId,
                model: skuItem.skuNameOfLang,
                specsName1: skuItem.specsList[0].specsValueOfLang,
                specsName2: skuItem.specsList[1].specsValueOfLang
              })
            }
          })
        }
      }
      categoryItemList[0].columnsList = columnsList1
      categoryItemList[0].tableData = tableData1
      categoryItemList[1].columnsList = columnsList2
      categoryItemList[1].tableData = tableData2
    } catch (error) {
      console.log(error)
    }
    return {
      allCategoryList,
      categoryItemList
    }
  },
  props: {},
  computed: { ...mapState(["locale"]) },
  async mounted() {
    this.getPoint()
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 0) {
        return "warning-row"
      } else {
        return "success-row"
      }
      return ""
    },
    handleSubItem(val, num, subIndex) {
      let columns = [],
        tableData = []
      if (Array.isArray(val) && val.length) {
        columns.push({ label: val[0].skuTitle, prop: "model" })
        val[0].specsList.map((specsItem, index) => {
          columns.push({ label: specsItem.specsNameOfLang, prop: `specsName${index + 1}` })
        })
        val.map((item) => {
          if (Array.isArray(item.specsList)) {
            if (item.specsList.length > 2) {
              tableData.push({
                skuId: item.skuId,
                model: item.skuNameOfLang,
                specsName1: item.specsList[0].specsValueOfLang,
                specsName2: item.specsList[1].specsValueOfLang,
                specsName3: item.specsList[2].specsValueOfLang
              })
            } else {
              tableData.push({
                skuId: item.skuId,
                model: item.skuNameOfLang,
                specsName1: item.specsList[0].specsValueOfLang,
                specsName2: item.specsList[1].specsValueOfLang
              })
            }
          }
        })
      }
      this.categoryItemList[num].columnsList = columns
      this.categoryItemList[num].tableData = tableData
      this.categoryItemList[num].activeIndex = subIndex
    },
    async getPoint() {
      let params = await this.$optionPoint.sendUrl(window, navigator, document)
      this.$api.burySave({ ...params }).then((res) => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.prodects-index {
  .product-category {
    padding: 20px 0;
    .product-category-item {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .category-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        cursor: pointer;
        .product-image {
          width: 90px;
          height: 80px;
          img {
            width: 100%;
            height: 100%;
          }
          .default {
            display: block;
          }
          .hover {
            display: none;
          }
        }
        .category-name {
          font-size: 16px;
          color: #333;
          line-height: 16px;
        }
        &:hover {
          .product-image {
            .default {
              display: none;
            }
            .hover {
              display: block;
            }
          }
          .category-name {
            color: #d71519;
          }
        }
      }
    }
  }
  .video-banner-wrap {
    width: 100%;
    max-width: 1420px;
    margin: 0 auto;
    overflow: hidden;
    .video-banner {
      width: 100%;
      padding-bottom: 41.7%;
      position: relative;
      video {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        height: 100%;
        width: auto;
        background-image: url("https://yunlian-oss-product.oss-cn-hangzhou.aliyuncs.com/website/p.jpg");
        background-size: cover;
        background-position: center;
      }
    }
  }
  .category-detail {
    margin-top: 30px;
    margin-bottom: 50px;
    .category-item {
      &:nth-child(1) {
        margin-bottom: 30px;
      }
      .item-title {
        font-weight: 600;
        font-size: 22px;
        color: #333;
        border-left: 3px solid #fb463f;
        padding-left: 5px;
      }
      .list-icon {
        margin: 0 auto;
        display: flex;
        background: #fff;
        justify-content: space-around;
        margin-top: 20px;
        &:hover {
          box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.16);
        }
        .icon-outer {
          cursor: pointer;
          &:hover {
            .icon-one span {
              color: #dc000c;
            }
            .icon-line {
              background: #dc000c;
            }
          }
          .icon-one {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin: 5px 0;
            img {
              height: 70px;
            }
            span {
              color: #333;
              font-size: 16px;
              line-height: 20px;
              transition-duration: 0.5s;
            }
            .activeText {
              color: #dc000c;
            }
          }
          .icon-line {
            width: calc(100% - 60px);
            margin-left: 30px;
            height: 2px;
            margin-top: 5px;
            background: #fff;
            transition-duration: 0.5s;
          }
          .activeLine {
            background: #dc000c;
          }
        }
      }
      .spece-compare {
        font-weight: 600;
        font-size: 18px;
        color: #333;
        text-align: center;
        margin-top: 30px;
      }
      .list-table {
        margin-top: 30px;
        border: 1px solid #ebebeb;
        ::v-deep .el-table {
          th .cell {
            font-weight: 700;
            font-size: 16px;
            color: #333;
            height: 30px;
            line-height: 30px;
          }
          td .cell {
            font-size: 14px;
            color: #504f4f;
            height: 30px;
            line-height: 30px;
          }
          .warning-row {
            background: #f2f7fd;
          }
          .success-row {
            background: #fff;
          }
        }
        .arrow-right {
          font-size: 18px;
          color: #504f4f;
          padding: 0 50px;
          &:hover {
            color: #dc000c;
            font-weight: 700;
            transform: scale(1.3);
            -webkit-transform: scale(1.3);
          }
        }
      }
    }
  }
}
.prodects-dropdown-menu {
  width: 700px;
  max-height: 350px;
  padding: 10px;
  box-sizing: border-box;
  .dropdown-menu {
    overflow: hidden;
    .menu-title {
      display: flex;
      justify-content: space-between;
      p {
        flex: 1;
        font-size: 14px;
        font-weight: 700;
        color: #333;
        line-height: 20px;
      }
    }
    .menu-content {
      max-height: 300px;
      overflow-y: auto;
      &::-webkit-scrollbar {
        display: none;
      }
      .content-item {
        display: flex;
        align-items: center;
        padding: 10px 0;
        .content-img {
          width: 100px;
          height: 100%;
          display: flex;
          flex-direction: column;
          align-items: center;
          flex-shrink: 0;
          .sub-img {
            width: 60px;
          }
          .sub-name {
            font-size: 12px;
            color: #333;
            line-height: 16px;
            text-align: center;
            margin-top: 5px;
          }
        }
        .content-models {
          width: 100%;
          min-height: 50px;
          display: flex;
          flex-wrap: wrap;
          justify-content: space-between;
          border-left: 1px solid #dcdfe6;
          margin-left: 10px;
          padding-left: 10px;
          box-sizing: border-box;
          &::after {
            content: "";
            width: 170px;
          }
          .models-sku {
            width: 170px;
            font-size: 12px;
            color: #333;
            line-height: 12px;
            margin: 6px 0;
            &:hover {
              color: #fb463f;
              text-decoration: underline;
              transform: scale(1.02);
              -webkit-transform: scale(1.02);
            }
          }
        }
      }
    }
  }
}
</style>
